/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.mzk.rehan.commons;

import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author Martin Řehánek <rehan at mzk.cz>
 */
public class StringUtils {

    private static final Map<Character, Character> diacriticMap = new HashMap<Character, Character>();
    private static final String TRIM_DELIMITER = "...";

    static {
        diacriticMap.put('á', 'a');
        diacriticMap.put('é', 'e');
        diacriticMap.put('í', 'i');
        diacriticMap.put('ó', 'o');
        diacriticMap.put('ú', 'u');
        diacriticMap.put('ý', 'y');
        diacriticMap.put('č', 'c');
        diacriticMap.put('ď', 'd');
        diacriticMap.put('ě', 'e');
        diacriticMap.put('ň', 'n');
        diacriticMap.put('ř', 'r');
        diacriticMap.put('š', 's');
        diacriticMap.put('ť', 't');
        diacriticMap.put('ž', 'z');
        diacriticMap.put('ů', 'u');
    }

    public static String removeDiacritics(String src) {
        char[] chars = src.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            Character withoutDiacritic = diacriticMap.get(chars[i]);
            if (withoutDiacritic != null) {
                chars[i] = withoutDiacritic.charValue();
            }
        }
        return new String(chars);
    }

    public static String removeNonIsoCharacters(String src) {
        char[] chars = src.toCharArray();
        StringBuilder result = new StringBuilder(chars.length);
        for (int i = 0; i < chars.length; i++) {
            char character = chars[i];
            if (Character.isISOControl(character)) {
                result.append(character);
            }
        }
        return result.toString();
    }

    public static String removeUndefinedUnicodeCharacters(String src) {
        char[] chars = src.toCharArray();
        StringBuilder result = new StringBuilder(chars.length);
        for (int i = 0; i < chars.length; i++) {
            char character = chars[i];
            if (Character.isDefined(character)) {
                result.append(character);
            }
        }
        return result.toString();
    }

    public static String trimString(String string, int maxLength, boolean addThreeDots) {
        if (string.length() <= maxLength) {
            return string;
        } else if (addThreeDots) {
            return string.substring(0, maxLength - TRIM_DELIMITER.length()) + TRIM_DELIMITER;
        } else {
            return string.substring(0, maxLength);
        }
    }

    public static String removeNonNumbers(String src) {
        char[] chars = src.toCharArray();
        StringBuilder result = new StringBuilder(chars.length);
        for (int i = 0; i < chars.length; i++) {
            char character = chars[i];
            if (Character.isDigit(character)) {
                result.append(character);
            }
        }
        return result.toString();
    }

    public static int countCharacters(String string, char characterToCount) {
        int counter = 0;
        for (char character : string.toCharArray()) {
            if (character == characterToCount) {
                counter++;
            }
        }
        return counter;
    }
}
